<script setup name="WorkFlowSearch">
import {WorkFlowGetLineList} from "@/api/sys/workflow";
import {getCurrentInstance, reactive} from "vue";
const {proxy} = getCurrentInstance();
const {hrp_user_sex, sys_is} = proxy.useDict('hrp_user_sex', 'sys_is');

const props = defineProps({
  // 字典名称
  title: {
    type: String,
    default: "样例查询"
  },
  // 是否单选
  isSingle: {
    type: Boolean,
    default: true
  },
  isButton: {
    type: Boolean,
    default: false
  },
  // 数据参数
  workflow: {
    type: Object,
    default: null
  }
});

let pd = reactive({
  workflowList: [],
  open: false,
  loading: true,
  total: 0,
  inputText: "",
  reParams: [],
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    code: undefined
  }
})

// 显示弹框
function show() {

  pd.queryParams = {...pd.queryParams, ...props.workflow};
  getList();
  pd.open = true;
}

function select1(selection, row) {
  //是否为单选模式
  if (props.isSingle) {
    //清空选项
    proxy.$refs["refTable"].clearSelection();
  }
  //将这行选中
  proxy.$refs["refTable"].toggleRowSelection(row, true);
}

function selectAll(selection) {
  //是否为单选模式
  if (props.isSingle) {
    return
    //清空选项
    proxy.$refs["refTable"].clearSelection();
  }
}

// 查询表数据
function getList() {
  pd.loading = true;
  WorkFlowGetLineList(pd.queryParams).then(res => {
    pd.workflowList = res.rows;
    pd.total = res.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

defineExpose({
  show,
});
const emit = defineEmits(["ok"]);

/** 选择操作 */
function handleSelected() {
  pd.reParams = proxy.$refs["refTable"].getSelectionRows();
  if (pd.reParams.length === 0) {
    proxy.$modal.msgError("请选择数据，至少一行!!");
    return;
  }
  pd.reParams.forEach(function (item) {
    pd.inputText = item.name;
  });
  pd.open = false;
  emit("ok", pd.reParams);
}
</script>

<template>
  <el-button v-if="props.isButton === true" type="primary" @click="show">{{ props.title }}</el-button>
  <el-input v-if="props.isButton === false" :style="{ width: '200px' }" readonly v-model="pd.inputText"
            :placeholder="props.title">
    <template #prepend>
      <el-button icon="Search" size="small" @click="show"></el-button>
    </template>
  </el-input>

  <!-- 字典搜索对话框 -->
  <el-dialog :title="props.title" v-model="pd.open" width="900px" append-to-body>
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true">
      <el-form-item label="数据查询" prop="dictLabel">
        <el-input v-model="pd.queryParams.name" placeholder="请输入名称" style="width: 200px"
                  @keyup.enter="handleQuery"/>
        <el-input v-model="pd.queryParams.code" placeholder="请输入手机号" style="width: 200px"
                  @keyup.enter="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜 索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重 置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table v-loading="pd.loading" @select="select1" @select-all="selectAll" ref="refTable" :data="pd.workflowList"
                height="260px">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="id" align="center" prop="id"/>
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="性别" align="center" prop="sex">
          <template #default="scope">
            <dict-tag :options="hrp_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="sys_is" :value="scope.row.state"/>
          </template>
        </el-table-column>
        <el-table-column label="启用" align="center" prop="enableFlag">
          <template #default="scope">
            <dict-tag :options="sys_is" :value="scope.row.enableFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark"/>
        <el-table-column label="创建人" align="center" prop="createBy"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人" align="center" prop="updateBy"/>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="pd.total > 0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                  v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleSelected">确 定</el-button>
        <el-button @click="pd.open = false">关 闭</el-button>
      </div>
    </template>
  </el-dialog>
</template>
